<?php

namespace App\Http\Controllers\Article\Merchant;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Article\ArticleValidate;
use App\Jobs\Article\ArticleOperate;
use App\Models\Article\ArticleStatisticsLogsModel;
use App\Service\Article\ArticleOperateService;
use App\Service\Article\ArticleService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ArticleController extends BaseController
{

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function indexList(Request $request): JsonResponse
    {
        $params = ArticleValidate::apiIndexListDetailValidate($request->all());
        return $this->success(ArticleService::indexList($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function operateList(Request $request): JsonResponse
    {
        $params = ArticleValidate::apiOperateListDetailValidate($request->all());
        return $this->success(ArticleService::operateList($params));
    }

    /**
     * @param int $id
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function detail(int $id): JsonResponse
    {
        ArticleValidate::apiDetailValidate(["id" => $id]);

        return $this->success(ArticleService::detail($id));
    }

    /**
     * 商家-交互操作(点赞、分享、收藏)
     * @param Request $request
     */
    public function operate(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'type'       => 'required|integer|' . Rule::in(array_keys(ArticleStatisticsLogsModel::TYPE_TEXT)),
                'articleId'  => 'integer',
                'articleIds' => 'array',
            ]
        );
        $userId = getRmsUserId();

        // 异步处理
        dispatch(
            (new ArticleOperate(
                [
                    'type'       => $request->type,
                    'articleId'  => $request->articleId ?? 0,// 单个文章id
                    'articleIds' => $request->articleIds ?? [],// 多个文章id
                    'userId'     => $userId,
                ]
            ))
        )->onQueue(ArticleOperate::QUEUE_NAME);

//        ArticleOperateService::operate($request->type, $request->articleId, $userId);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function videoList(Request $request): JsonResponse
    {
        $params = ArticleValidate::videoListValidate($request->all());
        return $this->success(ArticleService::videoList($params));
    }

    /**
     * 首页内容tab
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function tab(Request $request): JsonResponse
    {
        return $this->success(ArticleService::tab());
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function tabContent(Request $request): JsonResponse
    {
        $params = ArticleValidate::tabContentValidate($request->all());
        return $this->success(ArticleService::tabContent($params));
    }

    /**
     * 商家-首页数据埋点记录
     * @param Request $request
     */
    public function click(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'type' => 'required|integer|' . Rule::in(array_keys(ArticleOperateService::ARTICLE_DATA_TYPE_TEXT_MAPPING)),
            ]
        );

        $type = (int)$request->get('type', 0); // 1:banner 2:专题 3:内容
        return $this->success(ArticleOperateService::click($type));
    }
}
